package com.chee.plant.server.back;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.chee.plant.hibernate.object.Battle;
import com.chee.plant.server.HibernateSessionFactory;

@SuppressWarnings("unchecked")
public class BattleServer {
	public BattleServer(){
		this.initialize();
	}
	/**
	 * 初始化加载所有未完成的战役
	 */
	private void initialize(){
		// get battles from database first
		Session session = HibernateSessionFactory.getSession();
		String hql = "select * from Battle where endTime>current_timestamp() order by id desc";
		Query query = session.createQuery(hql);
		List<Battle> list = query.list();
		// for each battle , do battles
		Iterator<Battle> iter = list.iterator();
		while(iter.hasNext()){
			this.doBattle(iter.next());
		}
	}
	private void doBattle(Battle battle){
		// get plants
		Session session = HibernateSessionFactory.getSession();
		String hql = "select * from BattlefieldPlant where userId>current_timestamp() order by id desc";
		Query query = session.createQuery(hql);
		List<Battle> list = query.list();
		// for each battle , do battles
		Iterator<Battle> iter = list.iterator();
		while(iter.hasNext()){
			this.doBattle(iter.next());
		}
		// get corpse
	}
}

